#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>

#include "Aquarium.h"

#define BUFFER_SIZE 256
typedef std::vector< std::string > stringVec;

int _tmain(int argc, _TCHAR* argv[])
{
	std::string command = "";
	char buffer[BUFFER_SIZE];
	stringVec tokens;

	std::cout << "Bemvindo ao Dynamic Aquarium Generator!" << std::endl << std::endl;
	
	while( command == "" )
	{
		std::cout << "Digite seu comando:" << std::endl << std::endl;

		std::cin.getline(buffer, BUFFER_SIZE);
		command = buffer;
	}

	while( 1 )
	{
		tokens.push_back( command.substr( 0 , command.find(" ") ) );

		if( command.find(" ") == -1 )
			break;
		else
			command = command.substr( command.find(" ") + 1 );
	}

	command = tokens.front();
	tokens.erase( tokens.begin() );

	if( command == "exit" )
		exit(1);
	else if( command == "createnew" )
	{
		Aquarium mAquarium;

		if( !tokens.empty() )
		{
			mAquarium.setName( tokens.front() );
			tokens.erase( tokens.begin() );
		}

		tokens.clear();

		while( 1 )
		{
			command = "";
			while( command == "" )
			{
				std::cout << std::endl << "Digite proximo comando:" << std::endl << std::endl;

				std::cin.getline(buffer, BUFFER_SIZE);
				command = buffer;
			}

			while( 1 )
			{
				tokens.push_back( command.substr( 0 , command.find(" ") ) );

				if( command.find(" ") == -1 )
					break;
				else
					command = command.substr( command.find(" ") + 1 );
			}

			command = tokens.front();
			tokens.erase( tokens.begin() );

			if( command == "exit" )
				exit(1);
			else if( command == "size" )
			{
				float a_width = 0.0f;
				float a_height = 0.0f;
				float a_depth = 0.0f;
				bool has_parameters = false;

				if( !tokens.empty() )
				{
					a_width = atof( tokens.front().c_str() );
					tokens.erase( tokens.begin() );

					if( !tokens.empty() )
					{
						a_height = atof( tokens.front().c_str() );
						tokens.erase( tokens.begin() );

						if( !tokens.empty() )
						{
							a_depth = atof( tokens.front().c_str() );
							tokens.erase( tokens.begin() );
						}
					}

					if( a_width != 0.0f )
						mAquarium.setSizeW( a_width );
					if( a_height != 0.0f )
						mAquarium.setSizeH( a_height );
					if( a_depth != 0.0f )
						mAquarium.setSizeD( a_depth );

					has_parameters = true;
				}

				if( !has_parameters )
					std::cout << "Parametros nao informados!" << std::endl;
			}
			else if( command == "fish" )
			{
				bool has_parameters = false;

				if( !tokens.empty() )
				{
					std::string fish_name = "Peixe";
					float fish_scale = 1.0f;
					std::string modelpath = "fish_models/";
					modelpath.append( tokens.front() );
					modelpath.append(".wrl");
					tokens.erase( tokens.begin() );

					FILE *m_file;
					m_file = fopen( modelpath.c_str(), "rb" );
					if( m_file == NULL )
						exit(2);
					fclose( m_file );

					if( !tokens.empty() )
					{
						fish_name = tokens.front();
						tokens.erase( tokens.begin() );

						if( !tokens.empty() )
						{
							fish_scale = atof( ( tokens.front() ).c_str() );
							tokens.erase( tokens.begin() );
						}
					}

					mAquarium.addFish( fish_name, modelpath, fish_scale );

					has_parameters = true;
				}

				if( !has_parameters )
					std::cout << "Parametros nao informados!" << std::endl;
			}
			else if( command == "fishNb" )
			{
				mAquarium.addFish();
			}
			else if( command == "decorationNb" )
			{
				mAquarium.addDecoration();
			}
			else if( command == "done" )
				break;
		}

		//TODO: create files
		mAquarium.generateFiles();
	}

	system("pause");
	return 0;
}